package com.zbkj.front.controller;

import com.zbkj.common.request.user.ClientUserLoginRequest;
import com.zbkj.common.request.user.ClientUserSmscodeLoginRequest;
import com.zbkj.common.response.CommonResult;
import com.zbkj.common.response.user.ClientUserLoginResponse;
import com.zbkj.common.token.FrontTokenComponent;
import com.zbkj.common.vo.ValidateCode;
import com.zbkj.front.service.UserHandleService;
import com.zbkj.service.service.ValidateCodeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @Auther:
 * @Date: 2024/7/4 16:12
 * @Description:
 */
@CrossOrigin("*")
@Slf4j
@RestController
@RequestMapping("api/front/login")
@Api(tags = "用户登录")
public class UserLoginController {

    @Autowired
    private UserHandleService userHandleService;
    @Autowired
    private ValidateCodeService validateCodeService;
    @Autowired
    private FrontTokenComponent frontTokenComponent;

    @ApiOperation(value="用户登录")
    @PostMapping(value = "/userLogin")
    public CommonResult<ClientUserLoginResponse> userLogin(@RequestBody @Validated ClientUserLoginRequest loginRequest) {
        return CommonResult.success(userHandleService.userLogin(loginRequest));
    }

    @ApiOperation(value="用户登录")
    @PostMapping(value = "/userSmsCodeLogin")
    public CommonResult<ClientUserLoginResponse> userSmsCodeLogin(@RequestBody @Validated ClientUserSmscodeLoginRequest loginRequest) {
        return CommonResult.success(userHandleService.userSmsCodeLogin(loginRequest));
    }

    /**
     * 获取图片验证码
     * @return CommonResult
     */
    @ApiOperation(value="获取图片验证码")
    @GetMapping(value = "/validateCodeGet")
    public CommonResult<ValidateCode> get() {
        ValidateCode validateCode = validateCodeService.get(1);
        return CommonResult.success(validateCode);
    }

    @ApiOperation(value="退出登录")
    @GetMapping(value = "/loginOut")
    public CommonResult loginOut(HttpServletRequest request) {
        frontTokenComponent.logout(request);
        return CommonResult.success();
    }

    @ApiOperation(value="用户登录")
    @PostMapping(value = "/userLoginByName/{account}")
    public CommonResult<ClientUserLoginResponse> userLoginByName(@PathVariable("account") String account) {
        return CommonResult.success(userHandleService.userLoginByName(account));
    }
}
